Option Strict Off
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Diagnostics
Imports System.Threading
Imports MatrixDataStructures



Public Class CustomFileOpen
    Dim ofd1 As New OpenFileDialog
    Dim ex As New ShellClass


    Private Sub AIRbasedRegistration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ofd1.Filter = "Raw Ascii (*.asc)|*.asc|Raw Binary (*.bin)|*.bin|Acr/Nema (*.ima)|*.ima|Concorde uPET (*.img.hdr)|*.img.hdr|DICOM (*.dcm)|*.dcm|CTI ECAT 6 (*.img)|*.img|Gif89a (*.gif)|*.gif|InterFile (*.h33)+(*.i33)|*.h33+*.i33|INW-RUG (*.im)|*.im|NIfTI (*.nii)|*.nii|PNG (*.png)|*.png"
        ofd1.Title = "Choose the file"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ofd1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = ofd1.FileName
        End If
    End Sub
    Dim tempfilename As String = System.IO.Path.GetTempFileName

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        logbox.Text += tempfilename & vbNewLine

        Dim command As String = "set XMEDCONDIR=" & My.Application.Info.DirectoryPath & "\XMedCon"
        logbox.Text += command & vbNewLine
        logbox.Select(logbox.Text.Length, 0)
        logbox.ScrollToCaret()
        ex.CaptureOutput = True
        ex.CaptureError = True
        ex.Execute(command)
        logbox.Text += vbNewLine
        logbox.Text += ex.Output & vbNewLine & vbNewLine
        command = "set XMEDCONLUT=" & My.Application.Info.DirectoryPath & "\XMedCon\etc"
        logbox.Text += command & vbNewLine
        logbox.Select(logbox.Text.Length, 0)
        logbox.ScrollToCaret()
        ex.CaptureOutput = True
        ex.CaptureError = True
        ex.Execute(command)
        logbox.Text += vbNewLine
        logbox.Text += ex.Output & vbNewLine & vbNewLine
        command = "set XMEDCONRC=" & My.Application.Info.DirectoryPath & "\XMedCon\etc\xmedconrc"
        logbox.Text += command & vbNewLine
        logbox.Select(logbox.Text.Length, 0)
        logbox.ScrollToCaret()
        ex.CaptureOutput = True
        ex.CaptureError = True
        ex.Execute(command)
        logbox.Text += vbNewLine
        logbox.Text += ex.Output & vbNewLine & vbNewLine
        command = "set PATH=%XMEDCONDIR%\BIN;%XMEDCONDIR%\LIB;%PATH%"
        logbox.Text += command & vbNewLine
        logbox.Select(logbox.Text.Length, 0)
        logbox.ScrollToCaret()
        ex.CaptureOutput = True
        ex.CaptureError = True
        ex.Execute(command)
        logbox.Text += vbNewLine
        logbox.Text += ex.Output & vbNewLine & vbNewLine

        command = Chr(34) & My.Application.Info.DirectoryPath & "\Xmedcon\bin\"
        command += "medcon.exe" & Chr(34) & " -w -o " & Chr(34) & System.IO.Path.GetTempPath & "\123" & Chr(34) & " -c " & Chr(34) & "anlz" & Chr(34) & " -b16 -little -spm -f " & Chr(34) & TextBox1.Text & Chr(34)
        logbox.Text += command & vbNewLine
        logbox.Select(logbox.Text.Length, 0)
        logbox.ScrollToCaret()
        ex.CaptureOutput = True
        ex.CaptureError = True
        ex.Execute(command)
        logbox.Text += vbNewLine
        logbox.Text += ex.Output & vbNewLine & vbNewLine
        logbox.Select(logbox.Text.Length, 0)
        logbox.ScrollToCaret()

        If System.IO.File.Exists(System.IO.Path.GetTempPath & "\123.hdr") Then
            Dim b As singleVolume = CommonNamespace.HdrImgReader.ReadFromHdrFile(System.IO.Path.GetTempPath & "\123.hdr")
            If MsgBox("Load into Anatomy ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                FormMain.AnatomyVolume = b
                FormMain.AnatomyDisplay.OpenFile(FormMain.AnatomyVolume, "Anatomy", True, False)
                FormMain.AddAnatomyHandles()
            ElseIf MsgBox("Load into fMRI ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                FormMain.fMRIVolume = b
                FormMain.fMRIDisplay.OpenFile(FormMain.fMRIVolume, "fMRI", True, False)
                FormMain.AddfMRIHandles()
            Else
                MsgBox("Diffusion and Perfusion data cannot be ported as such.")
            End If
            Me.Close()
        End If

    End Sub


End Class
